﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient; //SQL连接

namespace _6_01_ADO.NET基础
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //使用DataReader读取数据(1)连接数据库,(2)执行查询,(3)读取
            //1-编写连接字符串(Windows方式)
            //string connStr = "Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=TestDB;Data Source=.";
            //2-编写连接字符串(SQL 用户名密码)
            string connStr = "server=.;database=TestDB;uid=sa;pwd=200234.A";

            //2.创建链接对象 打开连接
            SqlConnection sqlc = new SqlConnection(connStr);
            sqlc.Open();
            //3.编写sql执行语句
            string sql = "select * from Member";
            //4.执行sql语句
            SqlCommand cmd = new SqlCommand(sql,sqlc);
            SqlDataReader data = cmd.ExecuteReader();
            //利用获取到的数据进行数据操作
            //等待数据读取完
            while (data.Read())
            {
                
                ListViewItem listViewItem = new ListViewItem(data["MemberId"].ToString());
                listViewItem.SubItems.Add(data["MemberAccount"].ToString());
                listViewItem.SubItems.Add(data["MemberPwd"].ToString());
                listViewItem.SubItems.Add(data["MemberName"].ToString());
                listViewItem.SubItems.Add(data["MemberPhone"].ToString());
                listView1.Items.Add(listViewItem);
            }
            data.Close();
            //显示人数 
            cmd.CommandText= "select count(*) from Member";
            //读出第一行第一列
            int count =(int )cmd.ExecuteScalar();
            this.lvMem.Text = "会员人数" +count ;
            sqlc.Close();
        }

    }
}
